package de.jmda.gen.java.impl;

import javax.validation.constraints.NotNull;

import de.jmda.gen.java.DeclaredMethodGenerator;
import de.jmda.gen.java.InstanceMethodGenerator;
import de.jmda.gen.java.DeclaredInstanceMethodGenerator;

public class DefaultInstanceMethodGenerator
		extends AbstractMethodGenerator
		implements InstanceMethodGenerator
{
	// make sure to override accessors so that they use this field
	private DeclaredInstanceMethodGenerator generator;

	public DefaultInstanceMethodGenerator()
	{
		this(null);
	}
	
	public DefaultInstanceMethodGenerator(
      DeclaredInstanceMethodGenerator generator)
  {
	  super(notNull(generator));

	  // TODO bekommt man das nicht auch ohne den cast hin?
	  this.generator =
	  		(DeclaredInstanceMethodGenerator) getDeclaredMethodGenerator();
  }

	@Override
	public DeclaredInstanceMethodGenerator getDeclaredInstanceMethodGenerator()
	{
		return generator;
	}

	@Override
	public void setDeclaredInstanceMethodGenerator(
			DeclaredInstanceMethodGenerator generator)
	{
		super.setDeclaredMethodGenerator(generator);
		this.generator = generator;
	}

	@Override
  public DeclaredMethodGenerator getDeclaredMethodGenerator()
  {
	  return getDeclaredInstanceMethodGenerator();
  }

	@Override
  public void setDeclaredMethodGenerator(DeclaredMethodGenerator generator)
  {
		if (false == (generator instanceof DeclaredInstanceMethodGenerator))
		{
			throw new IllegalArgumentException(
					"generator is not a " +
					DeclaredInstanceMethodGenerator.class.getName());
		}

		setDeclaredInstanceMethodGenerator(
				(DeclaredInstanceMethodGenerator) generator);
  }

	@Override
  public void setInterfaceMethodDeclarationGenerator(
      boolean isInterfaceMethodDeclarationGenerator)
  {
		generator.setInterfaceMethodGenerator(isInterfaceMethodDeclarationGenerator);
  }

	@Override
  public boolean isInterfaceMethodDeclarationGenerator()
  {
	  return generator.isInterfaceMethodGenerator();
  }

	@NotNull 
	private static DeclaredMethodGenerator notNull(
      DeclaredInstanceMethodGenerator generator)
  {
		if (generator == null)
		{
			generator = new DefaultDeclaredInstanceMethodGenerator();
		}

		return generator;
  }
}